﻿#include "CFtpServer.h"
#include "ui_CFtpServer.h"
#include <QFileDialog>
#include <QMessageBox>

CFtpServer::CFtpServer(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::CFtpServer)
{
    ui->setupUi(this);

    m_pTcpServer = new QTcpServer;
    connect(m_pTcpServer,SIGNAL(newConnection()),this,SLOT(slot_NewConnection()));
    m_pTcpServer->listen(QHostAddress::AnyIPv4, FTP_PORT);
}

CFtpServer::~CFtpServer()
{
    delete ui;
    if (nullptr != m_pTcpServer)
    {
        delete m_pTcpServer;
        m_pTcpServer = nullptr;
    }
    stopAllThread();
}

void CFtpServer::stopAllThread()
{
    for(int i=0; i<m_vecSubServerThread.size(); i++)
    {
        if (nullptr != m_vecSubServerThread[i] && m_vecSubServerThread[i]->isRunning())
        {
            m_vecSubServerThread[i]->stop();
            delete m_vecSubServerThread[i];
            m_vecSubServerThread[i] = nullptr;
        }
    }
    m_vecSubServerThread.clear();
}

void CFtpServer::on_startButton_clicked()
{
    if ("" == m_strWorkDir)
    {
        QMessageBox::warning(this, "Warning!", "Please select a working direcotry");
        return;
    }
    if(!bConnected)
    {
        if(ui->userEdit->text().size() == 0)
        {
            QMessageBox::warning(this, "Warning!", "Please input your username");
            return;
        }
        if(ui->passEdit->text().size() == 0)
        {
            QMessageBox::warning(this, "Warning!", "Please input your password");
            return;
        }

        m_sServerConfig.strUserName = ui->userEdit->text();
        m_sServerConfig.strPassWord = ui->passEdit->text();
        m_iMaxClient = ui->spinBox->value();
        m_sServerConfig.iMaxClient = m_iMaxClient;
        m_sServerConfig.bAllowAnony = ui->allowAnony->isChecked();
        m_sServerConfig.bConfiged = true;
        m_sServerConfig.strWorkDir = m_strWorkDir;

        bConnected = true;
        ui->startButton->setText(QStringLiteral("关闭FTP服务器"));
    }
    else
    {
        stopAllThread();
        m_iCurClient = 0;        
        bConnected = false;
        ui->startButton->setText(QStringLiteral("启动FTP服务器"));
    }
}

void CFtpServer::on_dirButton_clicked()
{
    m_strWorkDir = QFileDialog::getExistingDirectory(this, "Please select a working direcotry.");
    if ("" != m_strWorkDir)
    {
        ui->lineEdit->setText(m_strWorkDir);
    }
}

void CFtpServer::slot_NewConnection()
{
    if(!bConnected)
    {
        return;
    }
    if(m_iCurClient<m_iMaxClient)
    {
        QTcpSocket *pClientSocket = m_pTcpServer->nextPendingConnection();

        CServerThread* pServerThread = new CServerThread;
        pServerThread->m_pServer->m_pClientSocket = pClientSocket;
        pServerThread->m_pServer->m_sServerConfig = m_sServerConfig;
        bool bRes = pServerThread->m_pServer->init();
        if (false == bRes)
        {
            qDebug()<<"CServer init failed.";
            return;
        }
        pServerThread->start();
        m_vecSubServerThread.append(pServerThread);

        m_iCurClient = m_iCurClient+1;
        qDebug()<<"No" << m_iCurClient << " client socket received!";
    }
}
